package com.jerry.shop.controller;

import com.jerry.shop.Constant;
import com.jerry.shop.bean.Seller;
import com.jerry.shop.enums.ResultEnum;
import com.jerry.shop.service.SellerService;
import com.jerry.shop.utils.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 卖家用户
 */
@Controller
@RequestMapping("/seller")
public class SellerUserController {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private SellerService sellerService;

    @GetMapping("toLogin")
    public ModelAndView toLogin() {
        return new ModelAndView("/seller/login");
    }

    @PostMapping("/login")
    public ModelAndView login(@RequestParam("username") String username,
                               @RequestParam("password") String password,
                               HttpServletResponse response,
                               Map<String, Object> map) {

        //1. 使用用户名和密码登录
        Seller seller = sellerService.login(username, password);
        if (seller == null) {
            map.put("msg", ResultEnum.LOGIN_FAIL.getMessage());
            map.put("url", "/seller/order/list");
            return new ModelAndView("common/error");
        }

        String token = UUID.randomUUID().toString();
        Integer expire = Constant.TOKEN_EXPIRE;
        //2. 设置token至redis
        String redisToken = String.format(Constant.REDIS_TOKEN, token);
        saveToRedis(redisToken, token, expire);

        //3. 设置token至cookie
        String cookieToken = Constant.COOKIE_TOKEN;
        CookieUtil.set(response, cookieToken, token, expire);

        return new ModelAndView("redirect:/seller/order/list");

    }

    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request,
                                HttpServletResponse response,
                                Map<String, Object> map) {
        //1. 从cookie里查询
        String cookieToken = Constant.COOKIE_TOKEN;
        Cookie cookie = CookieUtil.get(request, cookieToken);
        if (cookie != null) {
            //2. 清除redis
            String redisToken = String.format(Constant.REDIS_TOKEN, cookie.getValue());
            deleteFromRedis(redisToken);
            //3. 清除cookie
            CookieUtil.set(response, cookieToken, null, 0);
        }

        map.put("msg", ResultEnum.LOGOUT_SUCCESS.getMessage());
        map.put("url", "/");
        return new ModelAndView("common/success", map);
    }

    public void saveToRedis(String key,
                            String value,
                            Integer timeout) {
        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }
    public void deleteFromRedis(String key) {
        redisTemplate.opsForValue().getOperations().delete(key);
    }
}
